/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.OrderDetailsModel;
import model.OrderModel;
import model.ProductModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class OrderDAO {
     public static ArrayList<OrderModel> getAllOrder(){
         ArrayList<OrderModel> orderList  = new ArrayList<OrderModel>();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllOrder()}");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                OrderModel orderModel = new OrderModel();
                orderModel.setId(rs.getString("ID"));
                orderModel.setAccID(rs.getString("AccountID"));
                orderModel.setDate(rs.getString("Date"));
                orderModel.setOrderDes(rs.getString("Description"));
                orderModel.setStatus(rs.getString("Status"));
                orderModel.setApprovedBy(rs.getString("ApprovedByUser"));
                orderModel.setApprovedDate(rs.getString("ApprovedDate"));
                orderModel.setLastUpdate(rs.getString("LastUpdate"));
                orderModel.setRejectReason(rs.getString("RejectReason"));
                orderModel.setCusName(rs.getString("CusName"));
                orderModel.setDiscount(rs.getString("Discount"));
                orderModel.setDealerName(rs.getString("DealerName"));
                orderModel.setTotalAmount(rs.getString("TotalAmount"));
                orderList.add(orderModel);
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return orderList;
     }

     public static ArrayList<OrderModel> getAllOrderByUserID(String userID){
         ArrayList<OrderModel> orderList  = new ArrayList<OrderModel>();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllOrderByUserID(?)}");
            csmt.setString("userID", userID);
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                OrderModel orderModel = new OrderModel();
                orderModel.setId(rs.getString("ID"));
                orderModel.setAccID(rs.getString("AccountID"));
                orderModel.setDate(rs.getString("Date"));
                orderModel.setOrderDes(rs.getString("Description"));
                orderModel.setStatus(rs.getString("Status"));
                orderModel.setApprovedBy(rs.getString("ApprovedByUser"));
                orderModel.setApprovedDate(rs.getString("ApprovedDate"));
                orderModel.setLastUpdate(rs.getString("LastUpdate"));
                orderModel.setRejectReason(rs.getString("RejectReason"));
                orderModel.setCusName(rs.getString("CusName"));
                orderModel.setDiscount(rs.getString("Discount"));
                orderModel.setDealerName(rs.getString("DealerName"));
                orderModel.setTotalAmount(rs.getString("TotalAmount"));
                orderList.add(orderModel);
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return orderList;
     }

     public static OrderModel getOrderByID(String userID){
         OrderModel orderModel = new OrderModel();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetOrderByOrderID(?)}");
            csmt.setString("id", userID);
            ResultSet rs = DataUtil.executeStore(csmt);
            if(rs.next()){
                orderModel.setId(rs.getString("ID"));
                orderModel.setAccID(rs.getString("AccountID"));
                orderModel.setDate(rs.getString("Date"));
                orderModel.setOrderDes(rs.getString("Description"));
                orderModel.setStatus(rs.getString("Status"));
                orderModel.setApprovedBy(rs.getString("ApprovedByUser"));
                orderModel.setApprovedDate(rs.getString("ApprovedDate"));
                orderModel.setLastUpdate(rs.getString("LastUpdate"));
                orderModel.setRejectReason(rs.getString("RejectReason"));
                orderModel.setCusName(rs.getString("CusName"));
                orderModel.setDiscount(rs.getString("Discount"));
                orderModel.setDealerName(rs.getString("DealerName"));
                orderModel.setTotalAmount(rs.getString("TotalAmount"));
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return orderModel;
     }

     public static boolean reviewOrder(OrderModel orderModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_reviewOrder(?,?,?,?)}");
            csmt.setString("orderID", orderModel.getId());
            csmt.setString("status", orderModel.getStatus());
            csmt.setNString("approvedID", orderModel.getApprovedBy());
            csmt.setNString("reason", orderModel.getRejectReason());
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
     }

     public static OrderDetailsModel addProductToShopCard(ProductModel productModel){
         OrderDetailsModel orderDetailModel = new OrderDetailsModel();
         try {
             orderDetailModel.setProductID(productModel.getId());
             orderDetailModel.setProductName(productModel.getProName());
             orderDetailModel.setPrice(productModel.getPrice());
             orderDetailModel.setNumber("1");
         } catch (Exception e) {
             e.printStackTrace();
         }
         return orderDetailModel;
     }

     public static boolean insertOrder(OrderModel orderModel){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_insertOrder(?,?,?)}");
            csmt.setString("acc", orderModel.getAccID());
            csmt.setString("totalAmount", orderModel.getTotalAmount());
            csmt.registerOutParameter("id", java.sql.Types.INTEGER);
            result = DataUtil.executeNonStore(csmt);
            if(result){
                String orderID = csmt.getString("id");
                ArrayList<OrderDetailsModel> orderDetailList = orderModel.getOrderDetailList();
                ArrayList<OrderDetailsModel> orderDetailListNew = new ArrayList<OrderDetailsModel>();
                for(OrderDetailsModel obj : orderDetailList ){
                    obj.setOrderID(orderID);
                    if(OrderDetailDAO.insertOrderDetails(obj)){
                        orderDetailListNew.add(obj);
                    }
                }
                orderModel.setOrderDetailList(orderDetailListNew);
            }
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }

     public static boolean updateOrder(OrderModel orderModel){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_updateOrder(?,?)}");
            csmt.setString("id", orderModel.getId());
            csmt.setString("totalAmount", orderModel.getTotalAmount());
            result = DataUtil.executeNonStore(csmt);
            if(result){
                String orderID = csmt.getString(orderModel.getId());
                ArrayList<OrderDetailsModel> orderDetailList = orderModel.getOrderDetailList();
                ArrayList<OrderDetailsModel> orderDetailListNew = new ArrayList<OrderDetailsModel>();
                for(OrderDetailsModel obj : orderDetailList ){
                    obj.setOrderID(orderID);
                    if(OrderDetailDAO.updateOrderDetails(obj)){
                        orderDetailListNew.add(obj);
                    }
                }
                orderModel.setOrderDetailList(orderDetailListNew);
            }
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }
}